//
//  SISSEnrollTenantDetailViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/18.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSEnrollTenantDetailViewController.h"
#import "SISSMorePopupView.h"
#import "SISSTenantView.h"
#import "SISSContractRightFillInCell.h"
#import "SISSTenantEnrollViewController.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSTabBarController.h"
#import "SISSRecommendTenantViewController.h"

@interface SISSEnrollTenantDetailViewController ()<UITableViewDataSource,UITableViewDelegate,SISSMorePopupViewDelegate>

/* tenantView */
@property (weak, nonatomic) SISSTenantView *tenantView;
/* tableView */
@property (weak, nonatomic) SISSBaseTableView *tableView;

/* 数据源数组 */
@property (strong, nonatomic) NSArray<SISSContractModel *> *dataArr;


@end

@implementation SISSEnrollTenantDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"租客详情";

    // 添加左侧的返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    // 添加右侧分享投诉按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
}

- (void)back {
    if (self.isChange) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        if (self.isRegistration) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSRecommendTenantViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
        }
       
    }
}

#pragma mark - 代理
#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger type = self.dataArr[indexPath.row].cellType;
    if (type == 1) {
        SISSContractRightFillInCell *cell = [tableView dequeueReusableCellWithIdentifier:contractRightFillInCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.row];
        cell.userInteractionEnabled = NO;
        return cell;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SISSBaseView *headerView = [[SISSBaseView alloc] init];
    headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *label = [[UILabel alloc] init];
    label.textColor = COLOR(0x334A60);
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    [headerView addSubview:label];
    
    CGFloat margin = 8.0;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView).offset(2 * margin);
        make.right.equalTo(headerView).offset(-2 * margin);
        make.centerY.equalTo(headerView);
    }];
    
    if (section == 0) {
        label.text = @"以下信息不对外公开：";
    } else {
        label.text = @"";
    }
    
    return headerView;
}

/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    if (self.tenantModel.traded == 1 || self.tenantModel.trading == 1) { // 已交易、交易中的只有分享
        self.isChange = NO;
    }
    NSArray *arr = self.isChange ? @[@"修改", @"删除", @"分享"] : @[@"分享"];
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:arr];
    moreView.delegate = self;
    [moreView show];
}



#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (!self.isChange) {
        
        [self share];
        return;
    }
    
    switch (index) {
            
        case 0: {
            SISSTenantEnrollViewController *vc = [[SISSTenantEnrollViewController alloc] init];
            vc.tenantModel = self.tenantModel;
            vc.isChange = self.isChange;
            vc.title = @"租客修改";
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
            
        case 1: {
            [self deleteHouse];
        }
            break;
            
        case 2: {
            [self share];
        }
            break;
            
        default:
            break;
    }
}

- (void)share {
    
    NSString *genderStr = self.tenantModel.sex == 2 ? @"女士" : @"先生";
    NSString *title = [NSString stringWithFormat:@"%@%@ 求租",[self.tenantModel.renterName substringToIndex:1],genderStr];
    NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",self.tenantModel.minPrice,self.tenantModel.maxPrice,self.tenantModel.region,self.tenantModel.regionDetail,self.tenantModel.houseType];
    NSString *webpageUrl = @"";
    if (self.tenantModel.rentType == 1) { // 线上
        webpageUrl = [NSString stringWithFormat:@"%@/#!/rentQuestDetail/%@",WEB_BASE_URL,self.tenantModel.ID];
    } else { // 线下
        webpageUrl = [NSString stringWithFormat:@"%@/#!/rentQuestOfflineDetail/%@",WEB_BASE_URL,self.tenantModel.ID];
    }
    
    [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:nil];
}

/**
 删除租客
 */
- (void)deleteHouse {
    
    [kNetworkManager deleteTenantOfflineWithTenantId:self.tenantModel.ID successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 删除成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"删除成功！" andTime:1.0 andColor:YES];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.deleteComplete) {
                    self.deleteComplete();
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTenantView *tenantView = [[SISSTenantView alloc] init];
    tenantView.tenantModel = self.tenantModel;
    [self.containerView addSubview:tenantView];
    self.tenantView = tenantView;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] init];
    tableView.estimatedRowHeight = 44;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = NO;
    tableView.scrollEnabled = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [tableView registerClass:[SISSContractRightFillInCell class] forCellReuseIdentifier:contractRightFillInCellReuseID];
    [self.containerView addSubview:tableView];
    self.tableView = tableView;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.tenantView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tenantView.mas_bottom).offset(2 * margin);
        make.height.equalTo(@180);
        make.left.right.bottom.equalTo(weakSelf.containerView);
    }];
}

- (void)setTenantModel:(SISSTenantModel *)tenantModel {
    _tenantModel = tenantModel;
    
    NSArray *arr = @[ @{@"cellType" : @1,
                        @"leftStr" : @"租客姓名：",
                        @"rightStr" : tenantModel.renterName},
                      @{@"cellType" : @1,
                        @"leftStr" : @"租客身份证号码：",
                        @"rightStr" : tenantModel.idNumber},
                      @{@"cellType" : @1,
                        @"leftStr" : @"租客手机号码：",
                        @"rightStr" : tenantModel.mobile}];
    
    self.dataArr = [NSArray yy_modelArrayWithClass:[SISSContractModel class] json:arr];
    
}

@end
